//
//  Pet_LoginHomeViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_LoginHomeViewController: BasicViewController {
    
    let backI = UIImageView.initImgView(img: "loginback")
    private lazy var appleButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage.init(named: "icon10"), for: .normal)
        button.titleLabel?.font = .setTypeFace(18.Fit,type: .bold)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setTitle(" Sign in with Apple", for: .normal)
        button.backgroundColor = .white
        button.layer.cornerRadius = 25
        button.addTarget(self, action: #selector(third_mate_action_didClickedControl_action_Login_actionInfo(sender:)), for: .touchUpInside)
        return button
    }()
    private lazy var signInButton: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = .setTypeFace(18.Fit,type: .bold)
        button.setTitleColor(mainColor, for: .normal)
        button.setTitle("SIGN IN", for: .normal)
        button.backgroundColor = .clear
        button.layer.cornerRadius = 25
        button.layer.borderWidth = 3
        button.layer.borderColor = mainColor.cgColor
        button.tag = 100
        return button
    }()
    private lazy var signUpButton: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = .setTypeFace(18.Fit,type: .bold)
        button.setTitleColor(UIColor.black, for: .normal)
        button.setTitle("SIGN UP", for: .normal)
        button.backgroundColor = mainColor
        button.layer.cornerRadius = 25
        button.tag = 101
        return button
    }()
    private lazy var termsBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(mainColor, for: .normal)
        btn.setTitle("Terms of Use", for: .normal)
        btn.titleLabel?.font = .setTypeFace(14,type: .medium)
        btn.addTarget(self, action: #selector(mate_action_didClickedControl_action_Terms_actionInfo), for: .touchUpInside)
        btn.contentHorizontalAlignment = .trailing
        return btn
    }()
    private lazy var andLabel: UILabel = {
        let label = UILabel()
        label.text = "&"
        label.textColor = mainColor
        label.font = UIFont.setTypeFace(15,type: .medium)
        label.textAlignment = .center
        return label
    }()
    private lazy var policyBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(mainColor, for: .normal)
        btn.setTitle("Privacy Policy", for: .normal)
        btn.titleLabel?.font = .setTypeFace(14,type: .medium)
        btn.addTarget(self, action: #selector(mate_action_didClickedControl_action_Policy_actionInfo), for: .touchUpInside)
        btn.contentHorizontalAlignment = .leading
        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.subviews {
            backI
            appleButton
            signInButton
            signUpButton
            termsBtn
            andLabel
            policyBtn

        }
        backI.fillContainer()
        backI.isUserInteractionEnabled = true
        
        andLabel.size(30).bottom(52.Fit)
        andLabel.centerHorizontally()
        
        termsBtn.height(30).width(320/2.Fit)
        termsBtn.Trailing == andLabel.Leading
        termsBtn.Bottom == andLabel.Bottom
        
        policyBtn.height(30).width(340/2.Fit)
        policyBtn.Leading == andLabel.Trailing
        policyBtn.Bottom == andLabel.Bottom
        
        signInButton.left(35.Fit).right(35.Fit).height(50)
        signInButton.Bottom == andLabel.Top - 15

        signUpButton.left(35.Fit).right(35.Fit).height(50)
        signUpButton.Bottom == signInButton.Top - 15

        appleButton.left(35.Fit).right(35.Fit).height(50)
        appleButton.Bottom == signUpButton.Top - 15
        
        let evc = EulaViewController()
        evc.modalPresentationStyle = .overFullScreen
        evc.callBlock = {
            
        }
        self.present(evc, animated: false)
        
        signInButton.addAct {
            let vc = Pet_SignInViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        signUpButton.addAct {
            let vc = Pet_SignUpViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }

    @objc private func third_mate_action_didClickedControl_action_Login_actionInfo(sender: UIButton) {
        
        MateKingAppleLoginTool.instence.show {[weak self] user, token in
            RT.appleLogininWith(emailNum: user) { info in
                if info.xz?.count == 0
                {
                    let vc = Pet_LoginInfoViewController()
                    self?.navigationController?.pushViewController(vc, animated: true)
                }else
                {
                    let str = GetInfo(key: "UserInfo") as! NSDictionary
                    let jsonData = InfoUtil.initInfo.deserialize(from: str)
                    USEINFOMODEL = jsonData!
                    let vc = Pet_TabbarViewController()
                    UIApplication.shared.keyWindow?.rootViewController = vc
                }
            }
        } failure: { error in
            Toast_View_Tool.show(type: .error, text: error.errorDescripton)
        }
    }

    
    @objc private func mate_action_didClickedControl_action_Terms_actionInfo(sender: UIButton) {
        let vc = WkWebViewController()
        vc.urlstr = termUrl
        vc.str = "Terms of use"
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @objc private func mate_action_didClickedControl_action_Policy_actionInfo(sender: UIButton) {
        let vc = WkWebViewController()
        vc.urlstr = policeUrl
        vc.str = "Privacy policy"
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }

}
